package com.colin.aiyi.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.colin.aiyi.R;
import com.colin.aiyi.adapter.OneSayAdapter;
import com.colin.aiyi.entity.BannerEntity;
import com.colin.aiyi.entity.BaseRequest;
import com.colin.aiyi.entity.YiJuAndYiShuoEntity;
import com.colin.aiyi.eventbus.EventFloatButton;
import com.colin.aiyi.ui.Home.teacherSay.IViewSayTeacher;
import com.colin.aiyi.ui.Home.teacherSay.ImpPSay;
import com.colin.aiyi.utils.MusicUtil;
import com.colin.aiyi.view.CustomLoadMoreView;
import com.flyco.tablayout.SegmentTabLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGABanner;

import static com.colin.aiyi.fragment.WordFragment.tabs;

/**
 * Created by hl on 2017/5/5.
 * 导师说，
 */
@Deprecated
public class SaySearchFragment extends BaseFragment implements
        IViewSayTeacher {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    Unbinder unbinder;
    BGABanner bannerGuideContent;
    int postion = 0;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.tablayout)
    SegmentTabLayout tablayout;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.text_search)
    TextView textSearch;
    @BindView(R.id.text_no_data)
    TextView textNoData;
    private TabLayout headView_Tab;
    private List<YiJuAndYiShuoEntity> myWordList = new ArrayList<>();
    private OneSayAdapter headerAndFooterAdapter;
    private String content = "";
    private CustomLoadMoreView customLoadMoreView;
    private ArrayList<String> tips;
    BaseRequest baseRequest = new BaseRequest();

    private int contentType = 1;  //1:人气 2：最新 ,已购买3
    private String searchContent = "";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            contentType = bundle.getInt("contentType", 0);
            searchContent = bundle.getString("searchContent");
        }
        View view = inflater.inflate(R.layout.fragment_layout_home, null);
        unbinder = ButterKnife.bind(this, view);
        iPresenter = new ImpPSay(this);
        swipeLayout.setOnRefreshListener(iPresenter);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        initView(inflater);

        return view;
    }


    void initView(LayoutInflater inflater) {
//        View headView = inflater.inflate(R.layout.part_layout_header_say_search, (ViewGroup) mRecyclerView.getParent(), false);
//        headView_Tab = (TabLayout) (headView).findViewById(R.id.tablayout);
//        editSearch= (TextView) headView.findViewById(R.id.edit_search);
//        textSearch= (TextView) headView.findViewById(R.id.text_search);
//        textNoData= (TextView) headView.findViewById(R.id.text_no_data);
        textSearch.setOnClickListener(iPresenter);
        headerAndFooterAdapter = new OneSayAdapter(myWordList, getContext());
        headerAndFooterAdapter.setOnItemClickListener(iPresenter);
        headerAndFooterAdapter.setOnItemChildClickListener(iPresenter);
        headerAndFooterAdapter.openLoadAnimation();
        headerAndFooterAdapter.setOnLoadMoreListener(iPresenter, mRecyclerView);
        customLoadMoreView = new CustomLoadMoreView();
        headerAndFooterAdapter.setLoadMoreView(customLoadMoreView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(headerAndFooterAdapter);
        tablayout.setOnTabSelectListener(iPresenter);
        switch (contentType){
            case 2:
                this.contentType = 0;
                break;
            case 1:
                this.contentType = 1;
                break;
            case 3:
                this.contentType = 2;
                break;
        }
        tablayout.setCurrentTab(contentType);
        editSearch.setText(searchContent);
        tablayout.setTabData(tabs);

        iPresenter.onTabSelect(contentType);
//        tablayout.setCurrentTab(contentType);
        hideSoftInputFromWindow(getContext(), editSearch);
    }

    @Override
    public void goToTop(boolean b) {
        mRecyclerView.smoothScrollToPosition(0);
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    public void onResume() {
        super.onResume();
        iPresenter.activity_list(baseRequest);
        hideSoftInputFromWindow(getContext(),editSearch);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventListner(EventFloatButton eventFloatButton) {
        mRecyclerView.smoothScrollToPosition(0);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateMusicEvent(MusicUtil.MuiscEvent muiscEvent) {
        switch (muiscEvent.getEventType()) {
            case MusicUtil.MEDIA_STATUS_START:

                break;
            case MusicUtil.MEDIA_STATUS_PREPARE:
            case MusicUtil.MEDIA_STATUS_PLAY:

                break;
            case MusicUtil.MEDIA_STATUS_PAUSE:

                break;
            case MusicUtil.MEDIA_STATUS_STOP:

            case MusicUtil.MEDIA_STATUS_COMLETION:

                break;
        }
    }


    @Override
    public List<YiJuAndYiShuoEntity> getData() {
        return myWordList;
    }

    @Override
    public void startNextAc(Intent intent) {
        startActivity(intent);
    }

    @Override
    public void pullMoreComlite(String... msg) {
        headerAndFooterAdapter.loadMoreComplete();
    }

    @Override
    public void pullMoreNoMore(String... msg) {
        headerAndFooterAdapter.loadMoreComplete();
        customLoadMoreView.setLoadMoreStatus(CustomLoadMoreView.STATUS_END);
    }

    @Override
    public void refreshNoData(String... msg) {
        textNoData.setVisibility(View.VISIBLE);
    }

    @Override
    public void refreshComplite(String... msg) {
        MusicUtil.getInstance().stop();
        swipeLayout.setRefreshing(false);
        swipeLayout.setEnabled(true);
        headerAndFooterAdapter.notifyDataSetChanged();
        customLoadMoreView.setLoadMoreStatus(CustomLoadMoreView.STATUS_DEFAULT);
        textNoData.setVisibility(View.GONE);
    }

    @Override
    public void bindBanner(BannerEntity bannerEntity) {

    }


    @Override
    public String getSearchContent() {
        String searchContent = editSearch.getText().toString();
        if (searchContent == null)
            searchContent = "";
        return searchContent;
    }

    @Override
    public int getPageType() {
        return ImpPSay.PAGE_SEARCH;
    }

    @Override
    public void removeSearchContent() {
        editSearch.setText("");
    }
}
